package com.kehutong.contact.util;

import com.kehutong.common.DoveClient;
import com.kehutong.contact.enums.HouseType;
import org.coraframework.json.JSONObject;
import org.coraframework.util.Ioc;
import org.coraframework.util.Objects;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public abstract class ResidentUtil {

    /**
     * 根据楼栋编号获取管家id列表
     *
     * @param buildNo     楼栋编号
     * @param houseTypeId 房屋类型id
     * @return 管家id列表
     */
    public static Set<String> getManagerIdList(String buildNo, String houseTypeId) throws Exception {
        JSONObject result = new JSONObject();
        DoveClient doveClient = Ioc.getInstance(DoveClient.class);
        if (Objects.equal(HouseType.HOUSE.getId(), houseTypeId)) {
            result = doveClient.post("/basic/building/service/get", (http) -> {
                JSONObject body = new JSONObject();
                body.put("id", buildNo);
                http.setBody(body.toJSONString());
            });
        } else if (Objects.equal(HouseType.SHOP.getId(), houseTypeId)) {
            result = doveClient.post("/basic/shop/classify/service/get", (http) -> {
                JSONObject body = new JSONObject();
                body.put("id", buildNo);
                http.setBody(body.toJSONString());
            });
        } else if (Objects.equal(HouseType.PARKING.getId(), houseTypeId)) {
            result = doveClient.post("/basic/parking/classify/service/get", (http) -> {
                JSONObject body = new JSONObject();
                body.put("id", buildNo);
                http.setBody(body.toJSONString());
            });
        }
        if (result.getIntValue("code") == 200) {
            JSONObject buildingJson = result.getJSONObject("data");
            if (Objects.nonNull(buildingJson)) {
                List<String> employeeNos = JSONObject.parseArray(buildingJson.getJSONArray("employeeNos").toJSONString(), String.class);
                return new HashSet<>(employeeNos);
            }
        }
        return new HashSet<>();
    }
}
